@extends('layout.adminlayout')
@section('css')
<script src="/admin/js/metisMenu.min.js"></script>
<script src="/admin/js/custom.js"></script>
<link href="/admin/css/area_info.css" rel="stylesheet">
<link href="/admin/css/custom.css" rel="stylesheet">
<link href="/admin/css/add_manage.css" rel="stylesheet">
<link href="/admin/css/select_building.css" rel="stylesheet">
@endsection
@section('content')
<div class="forms">
	<div class=" form-grids row form-grids-right">
		<div class="widget-shadow " data-example-id="basic-forms"> 
			<div class="form-title">
				<h4>设备编辑 :</h4>
			</div>
			<div class="form-body">
				<form action="/admin/building/store" method="post" class="form-horizontal" onsubmit="return fun4()">
					{{csrf_field()}}
					<div class="form-group">
						<label for="inputPassword3" class="col-sm-2 control-label">
							EUI:
						</label>
						<div class="col-sm-3">
							<input type="text" class="form-control" name="eui" id="eui" placeholder="room name" value="{{$data['eui']}}" disabled>
							<input type="hidden" name="" id="rd_id" value="{{$data['id']}}">
						</div>
					</div>
					<div class="form-group">
						<label for="inputEmail3" class="col-sm-2 control-label">
							区域 :
						</label>
						<li class="area_info">
							<div class="col-sm-3">
								<select name="area" id="area" class="form-control1">
									<option value="0">请选择区域</option>
									@if($area != null)
										@foreach($area as $v)
											<option value="{{$v['a_id']}}"
													@if($v['a_id'] == $data['a_id'])
													selected
													@endif
											>{{$v['a_name']}}</option>
										@endforeach
									@endif
								</select>
							</div>
						</li>
					</div>
					<div class="form-group">
						<label for="inputEmail3" class="col-sm-2 control-label">
							建筑 :
						</label>
						<li class="area_info">
							<div class="col-sm-3">
								<input type="text" name="buildings" class="form-control" value="{{$data['b_name']}}" placeholder="请选择">
								<input type="hidden" name="building" class="form-control" value="{{$data['b_id']}}" placeholder="请选择">
								<dl class="col-sm-6 dv4" id="building">
									@if($building != null)
										@foreach($building as $v)
											<option class="dstyle" value="{{$v['b_id']}}"
											>{{$v['b_name']}}</option>
										@endforeach
									@endif
								</dl>
							</div>
						</li>
					</div>
					<div class="form-group">
						<label for="inputEmail3" class="col-sm-2 control-label">
							房间 :
						</label>
						<li class="area_info">
							<div class="col-sm-3">
								<select name="room" id="rooms" class="form-control1">
									<option value="0">请选择房间</option>
									@if($room != null)
										@foreach($room as $v)
											<option value="{{$v['r_id']}}"
													@if($v['r_id'] == $data['r_id'])
													selected
													@endif
											>{{$v['r_name']}}</option>
										@endforeach
									@endif
								</select>
							</div>
						</li>
					</div>
					<div class="form-group">
						<label for="inputEmail3" class="col-sm-2 control-label">
							具体位置 :
						</label>
						<div class="col-sm-3">
							<input type="text" class="form-control" name="loc" placeholder="具体位置" id="loc" value="{{$data['location']}}">
						</div>
					</div>
					<input type="hidden" id="fri_type" name="fri_type" value="{{$data['type_id']}}">
					<div class="form-group">
						<label for="inputEmail3" class="col-sm-2 control-label">
							所属类型 :
						</label>
						<li class="area_info">
							<div class="col-sm-3">
								<select name="type" id="type" class="form-control1">
									<option value="0">请选择设备类型</option>
									@if($type != null)
										@foreach($type as $val)
											<option value="{{$val['id']}}"
													@if($val['id'] == $data['type_id'])
													selected
													@endif
											>{{$val['type']}}</option>
										@endforeach
									@endif
								</select>
							</div>
						</li>
					</div>
					<div class="form-group">
						<label for="inputEmail3" class="col-sm-2 control-label">
							设备型号 :
						</label>
						<li class="area_info">
							<div class="col-sm-3">
								<select name="model" id="model" class="form-control1">
									<option value="0">请选择设备型号</option>
									@if($model != null)
										@foreach($model as $v)
											<option value="{{$v['tp_id']}}"
													@if($v['tp_id'] == $data['model_id'])
													selected
													@endif
											>{{$v['t_model']}}</option>
										@endforeach
									@endif
								</select>
							</div>
						</li>
					</div>
					<div class="col-sm-offset-2">
						<button type="button" class="btn btn-default" id="sub" style="margin-left: 0.38rem">提交</button>
					</div>
				</form> 
			</div>
		</div>
	</div>
</div>
<script>
	//区域建筑联动
	$('#area').change(function () {
		var areaId = $('#area').val()
		$.ajax({
			type:'post',
			url:'/admin/room/getbuild',
            async:false,
            data:{
                "_token": $("input[name=_token]").val(),
                'areaId':areaId,
            },
            dataType:'json',
            success:function (data) {
                var str = '<option  class="dstyle" value="0">请选择建筑</option>';
                for(var i=0;i<data.length;i++){
                    str += '<option  class="dstyle" value="'+data[i]['b_id']+'">'+data[i]['b_name']+'</option>';
				}
                $('#rooms').empty();
                $('#rooms').append('<option value="0">请选择房间</option>');
                $('#building').empty();
                $('#building').append(str);
            }
		})
    })

	//建筑房间联动
    $('#rooms').focus(function () {
        var buildingId = $('input[name=building]').val();
        $.ajax({
            type:'post',
            url:'/admin/room/getroom',
            async:false,
            data:{
                "_token": $("input[name=_token]").val(),
                'buildingId':buildingId,
            },
            dataType:'json',
            success:function (data) {
                var option = '<option value="0">请选择房间</option>';
                for(var i=0;i<data.length;i++){
                    console.log(data[i]['r_id']);
                    option += '<option value="'+data[i]['r_id']+'">'+data[i]['r_name']+'</option>';
                }
                $('#rooms').empty();
                $('#rooms').append(option);
            }
        });
    });

	//类型与型号联动
    $('#type').change(function () {
        var typeId = $('#type').val();
        $.ajax({
            type:'post',
            url:'/admin/device/getmodel',
            async:false,
            data:{
                "_token": $("input[name=_token]").val(),
                'typeId':typeId,
            },
            dataType:'json',
            success:function (data) {
                var option = '<option value="0">请选择型号</option>';
                for(var i=0;i<data.length;i++){
                    console.log(data[i]['tp_id']);
                    option += '<option value="'+data[i]['tp_id']+'">'+data[i]['t_model']+'</option>';
                }
                $('#model').empty();
                $('#model').append(option);
            }
        });
    });
    //提交表单
    $('#sub').click(function () {
        var rd_id = $('#rd_id').val();
        var eui = $('#eui').val();
        var area = $('#area').val();
        var building = $('input[name=building]').val();
        var rooms = $('#rooms').val();
        var loc = $('#loc').val();
        var type = $('#type').val();
        var fri_type = $('#fri_type').val();
        var model = $('#model').val();
        if(eui != undefined && area != 0 && building !=0 && rooms != 0 && type != 0 && model != 0){
            $.ajax({
                type:'post',
                url:'/admin/device/edit',
                async:false,
                data:{
                    "_token": $("input[name=_token]").val(),
					'rd_id':rd_id,
					'eui':eui,
                    'area':area,
					'building':building,
					'rooms':rooms,
					'loc':loc,
					'type':type,
					'fri_type':fri_type,
                    'model':model
                },
                dataType:'json',
                success:function (data) {
                    location.href = '/admin/device/index';
                }
            })
        }else{
            alert('信息填写不完整或经纬度填写不合法')
            return false;
        }
    });
</script>
@endsection
@section('lastjs')
<script src="/admin/js/add_manage.js"></script>
<script src="/admin/js/select_building.js"></script>
@endsection